package com.demo.redis;

import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author LMH
 * @version 创建时间：2019年7月17日 下午3:53:55
 */
public class JedisClientPool implements JedisClient {

	private static JedisPool jedisPool = null;
//	private static Jedis jedis;

//	static {
//		jedis = getJedisPool().getResource();
////		System.out.println("静态加载jedis--> "+jedis);
//	}

	/**
	 * 构建redis连接池
	 */
	public static JedisPool getJedisPool() {
		if (jedisPool == null) {  
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(1024); // 可用连接实例的最大数目,如果赋值为-1,表示不限制.
			config.setMaxIdle(5); // 控制一个Pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8
			config.setMaxWaitMillis(1000 * 100); // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时/如果超过等待时间,则直接抛出异常
			config.setTestOnBorrow(true); // 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的
			jedisPool = new JedisPool(config, "127.0.0.1", 6379);
		}
		return jedisPool;
	}

	/**
	 * 释放jedis资源
	 */
	public static void returnResource(Jedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}

	@Override
	public String set(String key, String value) {
		String result = null;
		Jedis jedis = null;
		try {
			JedisPool pool = getJedisPool();
			jedis = pool.getResource();
			result = jedis.set(key, value);
		} catch (Exception e) {
			returnResource(jedis);
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return result;
	}

	@Override
	public String get(String key) {
		String value = null;
		Jedis jedis = null;
		try {
			JedisPool pool = getJedisPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			System.out.println("空指针？？");
			returnResource(jedis);
			e.printStackTrace();
		} finally {
			returnResource(jedis);
		}
		return value;
	}

	@Override
	public Boolean exists(String key) {
		Jedis jedis = getJedisPool().getResource();
		Boolean result = jedis.exists(key);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long expire(String key, int seconds) {
//		发现jedis定义成static的了，那每次都不会从线程池中获取新的，于是改为getJedisPool().getResource()
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.expire(key, seconds);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long ttl(String key) {
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.ttl(key);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long incr(String key) {
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.incr(key);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.hset(key, field, value);
		returnResource(jedis);
		return result;
	}

	@Override
	public String hget(String key, String field) {
		Jedis jedis = getJedisPool().getResource();
		String result = jedis.hget(key, field);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long hdel(String key, String... field) {
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.hdel(key, field);
		returnResource(jedis);
		return result;
	}

	@Override
	public Boolean hexists(String key, String field) {
		Jedis jedis = getJedisPool().getResource();
		Boolean result = jedis.hexists(key, field);
		returnResource(jedis);
		return result;
	}

	@Override
	public List<String> hvals(String key) {
		Jedis jedis = getJedisPool().getResource();
		List<String> result = jedis.hvals(key);
		returnResource(jedis);
		return result;
	}

	@Override
	public Long del(String key) {
		Jedis jedis = getJedisPool().getResource();
		Long result = jedis.del(key);
		returnResource(jedis);
		return result;
	}

	public static void main(String[] args) {
		JedisClientPool jedisClientPool = new JedisClientPool();
//		System.out.println(jedisClientPool.set("ceshi2", "====="));
//		System.out.println(jedisClientPool.get("name"));
		System.out.println(jedisClientPool.del("test"));
		
	}
}
